package com.grace.cms.common.enums;

public enum RecordTypeEnums {
    ARTICLE("article"),
    CATEGORY("category"),
    LINK("link"),
    CONTACT("contact"),
    COLUMN("column"),
    COMMENT("comment"),
    THEME("theme"),
    PAGE("page");

    private String prefix;
    RecordTypeEnums(String prefix) {
        this.prefix = prefix;
    }

    public String getPrefix(Object o) {
        if (o == null) {
            throw new IllegalArgumentException("Parameter 'o' cannot be null");
        }
        return String.format("%s_%s", getPrefix(), o);
    }

    public String getRealId(String o) {
        if (o == null) {
            throw new IllegalArgumentException("Parameter 'o' cannot be null");
        }
        return o.replace(getPrefix()+"_", "");
    }

    public String getPrefix() {
        return prefix;
    }
    
    /**
     * 根据序号获取对应的记录类型枚举值
     * 
     * @param ordinal 序号（从0开始）
     * @return 对应的记录类型枚举值
     * @throws IllegalArgumentException 如果序号无效
     */
    public static RecordTypeEnums valueOf(int ordinal) {
        if (ordinal < 0 || ordinal >= values().length) {
            throw new IllegalArgumentException("Invalid ordinal: " + ordinal + ", valid range: 0-" + (values().length - 1));
        }
        return values()[ordinal];
    }

    /**
     * 根据记录ID获取对应的记录类型枚举值
     * 
     * @param recordId 记录ID
     * @return 对应的记录类型枚举值
     * @throws IllegalArgumentException 如果记录ID无效
     */
    public static RecordTypeEnums getRecordType(String recordId) {
        if (recordId == null || recordId.isEmpty()) {
            throw new IllegalArgumentException("Record ID cannot be null or empty");
        }
        String prefix = recordId.split("_")[0];
        for (RecordTypeEnums type : values()) {
            if (type.getPrefix().equals(prefix)) {
                return type;
            }
        }
        throw new IllegalArgumentException("Invalid record ID prefix: " + prefix);
    }
  
}
